﻿var noSupportMessage = "浏览器不支持WebSocket，请选择谷歌浏览器、火狐、IE 10+浏览器或！";
var ws;
var type;
var iID = null;
var count = 0;//连接次数


function webSocketServerOpen()
{
    //关闭重新连接服务器状态
    if (iID != null) {
        iID = window.clearInterval(iID);
        count = 0;
    }
    sendMessage("LOGIN " + type); //注册客户端类型 LOGIN注册命令 控制端：wssCtName  受控端：wssClName
    webSocketStatus = 1;//服务器已连接
    $("#webSocketStatus").css("backgroundImage", "url(../../Content/Imgs/bglv.png)");
}

function webSocketServerClose() {
    if (iID == null)//重新连接服务器
        iID = self.setInterval("connectSocketServer()", 5000);
    count++;
    webSocketStatus = 0;
    $("#webSocketStatus").css("backgroundImage", "url(../../Content/Imgs/bgh.png)");
}
function webSocketServerOnMessage(evt) {
    webSocketStatus = 1;//服务器已连接
    $("#webSocketStatus").css("backgroundImage", "url(../../Content/Imgs/bglv.png)");
    if (validateTimer != null) {
        validateTimer = clearTimeout(validateTimer);
    }
}

//连接服务
function connectSocketServer() {
    var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);
    if (support == null) {
        alert("* " + noSupportMessage + "<br/>");
        return;
    }
    ws = new window[support]('ws://10.120.131.174:2112/');
    ws.onmessage = function (evt) {
        //接收指令
        
        var data;
        var json = evt.data;
        if (evt.data != "success") {
            eval("data=" + json);

            if (data.CommandName == "GisLocation") {
                if (data.ID == 0) {
                    var sCenter = data.Lon + "," + data.Lat;
                    var sLevel = data.ZoomLevel;
                    ZoomToPoint(sCenter, sLevel);
                }
                else {
                    var strss = new Array(); //定义一数组
                    strss = data.ID.split('|');
                    //ZoomToFeature("CSSDE.MK_PETROL_STATION", "LA23");
                }

                //alert(evt.data);

                //var strss = new Array(); //定义一数组
                //strss = evt.data.split(':');
                //if (strss[0] == "GIS") {
                //    var strs = new Array();
                //    strs = strss[1].split(',');
                //    var center = strs[0] + "," + strs[1];
                //    var level = strs[2] - 0;

                //    ZoomToPoint(center, level);
                //}


            }
        }
    };
    ws.onopen = function () {
        //服务开启
        webSocketServerOpen();
    };
    ws.onclose = function () {
        //服务关闭
        webSocketServerClose();
    }
}

//关闭服务
function disconnectWebSocket() {
    if (ws) {
        ws.close();
    }
}

//发送消息
function sendMessage(message) {
    if (ws) {
        ws.send(message);
    }
}


